package cs236369.hw5;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GetPosts
 * 
 * A servlet to bring posts to the map from the DB
 * Searches for bringWhat parameter. 0 to bring tasks, 1 to bring teams, 2 to bring matches,
 * 		3 to bring all the tasks of the user in session, 4 to bring all the teams of the user in session 
 * 		5 to search for tasks with parameters, 6 to search for teams with parameters,
 * 		7 to search for matches with parameters, 8 to bring a single post
 */
@WebServlet(description = "A servlet to bring posts for the map", urlPatterns = { "/GetPosts", "/GetPosts/*" })
public class GetPosts extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(request.getParameter("bringWhat") == null){
			response.sendRedirect(InitListener.welcomePage);
			return;
		}
		int bringWhat = Integer.parseInt(request.getParameter("bringWhat"));
		bringPosts(request,response,bringWhat);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

	private void bringPosts(HttpServletRequest request, HttpServletResponse response, int bringWhat) throws IOException{
		ArrayList<Post> postsList = new ArrayList<Post>();
		
		switch (bringWhat) {
		case 0:
			postsList = DBConnection.getAllMissions();
			break;
		case 1:
			postsList = DBConnection.getAllTeams();
			break;
		case 2:
			postsList = DBConnection.getAllMissions();
			postsList.addAll(DBConnection.getAllTeams());
			break;
		case 3:
		case 4:
			String username = request.getParameter("username");
			postsList = DBConnection.getAllMissions(username);
			postsList.addAll(DBConnection.getAllTeams(username));
			bringWhat = bringWhat - 3; //0 to bring tasks, 1 to bring teams
			break;
		case 5:
		case 6:
		case 7:
			if(!validateSearchParameters(request)){
				response.setStatus(400);
				response.getWriter().print("Illegal search parameter");
				return;
			}
			double latPos = Double.parseDouble(request.getParameter("latPos")); 
			double lngPos = Double.parseDouble(request.getParameter("lngPos"));
			double distance = Double.parseDouble(request.getParameter("radius"));
			bringWhat = bringWhat - 5; //0 to bring tasks, 1 to bring teams, 2 to bring both for matches
			postsList = DBConnection.searchPosts(latPos, lngPos, distance, bringWhat, null);
			break;
		case 8:
			if(!validateSinglePostParameters(request)){
				response.setStatus(400);
				response.getWriter().print("Illegal post ID");
				return;
			}
			int postID = Integer.parseInt(request.getParameter("postID"));
			int postType = Integer.parseInt(request.getParameter("postType"));
			Post p;
			if(postType==0){
				p = DBConnection.getMission(postID);
			}
			else {
				p = DBConnection.getTeam(postID);
			}
			postsList.add(p);
			bringWhat=postType;
			break;
		default:
			response.sendRedirect(InitListener.welcomePage);
			return;
		}
	
		try {
			response.setContentType("application/vnd.google-earth.kml+xml");
			XALANconverter.convertPostsToKML(XMLGenerator.getPostsXMLasInputStream(postsList, getServletContext()), 
					response.getOutputStream(), bringWhat, getServletContext());
			response.flushBuffer();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private boolean validateSearchParameters(HttpServletRequest request){
		try{
			if(request.getParameter("radius") == null){
				return false;
			}
			if(Double.parseDouble(request.getParameter("radius")) <= 0){
				return false;
			}	
			Double.parseDouble(request.getParameter("latPos")); 
			Double.parseDouble(request.getParameter("lngPos"));
			return true;
		} catch(Exception e){
			return false;
		}	
	}
	
	private boolean validateSinglePostParameters(HttpServletRequest request){
		try{
			if(request.getParameter("postID") == null || request.getParameter("postType") == null){
				return false;
			}
			if(Integer.parseInt(request.getParameter("postID")) < 0 || 
					Integer.parseInt(request.getParameter("postType")) < 0 || 
					Integer.parseInt(request.getParameter("postType")) > 1){
				return false;
			}	
			return true;
		} catch(Exception e){
			return false;
		}	
	}
	
}
